/**
* 微信支付回调
* @param params
* @return
*/
@RequestMapping("/wechatPayCallback")
public JSONObject wechatPayCallback(@RequestBody Map<String, Object> params) {

log.info("WechatPayCallback...");

JSONObject resourceJson = JSONUtil.parseObj(params.get("resource"));
log.info(resourceJson.toString());
try {
String decrypt = WechatUtil.decryptToString(resourceJson.getStr("associated_data").getBytes(), resourceJson.getStr("nonce").getBytes(), resourceJson.getStr("ciphertext"));
log.info(decrypt);

JSONObject decryptJson = JSONUtil.parseObj(decrypt);
String out_trade_no = decryptJson.getStr("out_trade_no");
// out_trade_no就是创建订单时的编号,下面就是写自己的支付逻辑
} catch (Exception e) {
log.info(e.getMessage());
e.printStackTrace();
}

return JSONUtil.createObj()
.set("code", "SUCCESS")
.set("message", "成功");
}
/**
* 解密
* @param associatedData
* @param nonce
* @param ciphertext
* @return
* @throws GeneralSecurityException
* @throws IOException
*/
public static String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException, IOException {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

SecretKeySpec key = new SecretKeySpec(API_V3_KEY.getBytes(), "AES");
GCMParameterSpec spec = new GCMParameterSpec(128, nonce);

cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);

return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}